package com.boboPlanet.dict.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.boboPlanet.comm.constance.DataSource;
import com.boboPlanet.comm.context.BusinessStartTimeContext;
import com.boboPlanet.comm.context.LoginUserContext;
import com.boboPlanet.comm.exception.BusinessException;
import com.boboPlanet.comm.utils.AutoGenerateIds;
import com.boboPlanet.comm.utils.DateUtil;
import com.boboPlanet.dict.mapper.LogMapper;
import com.boboPlanet.dict.service.LogService;
import com.boboPlanet.dict.entity.dto.SysLogDTO;
import com.boboPlanet.dict.entity.po.SysLogPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2023/2/23
 **/
@Service
@Slf4j
public class LogServiceImpl implements LogService {

    @Resource
    private LogMapper logMapper;

    @Override
    @DS(DataSource.DICT)
    public Boolean addSysLog(SysLogDTO dto) {
        ReadWriteLock lock = new ReentrantReadWriteLock();
        Lock writeLock = lock.writeLock();
        writeLock.lock();
        try {
            String errMsg = "日志内容/日志系统不能为空...";
            if (!Optional.ofNullable(dto).isPresent()){
                throw BusinessException.build(errMsg);
            }
            if (StringUtils.isBlank(dto.getLogContent()) || Objects.equals(dto.getLogContent(), "")){
                throw BusinessException.build(errMsg);
            }
            if (StringUtils.isBlank(dto.getLogSys()) || Objects.equals(dto.getLogSys(), "")){
                throw BusinessException.build(errMsg);
            }
            if (StringUtils.isBlank(dto.getLogUser()) || Objects.equals(dto.getLogUser(), "")){
                dto.setLogUser(LoginUserContext.getLoginUserInfoPO().getUserName());
            }
            if (dto.getLogTime() == null){
                dto.setLogTime(BusinessStartTimeContext.getTime());
            }

            SysLogPO po = new SysLogPO();
            po.setLogId(AutoGenerateIds.getGenerateId());
            po.setLogUser(dto.getLogUser());
            po.setLogContent(dto.getLogContent());
            po.setLogTime(dto.getLogTime());
            po.setLogSys(dto.getLogSys());
            po.setVersion(0);

            return logMapper.insert(po) > 0;

        }catch (Exception e){
            log.error("添加日志失败: {}",e.getMessage());
        }finally {
            writeLock.unlock();
        }
        return false;

    }

    @Override
    @DS(DataSource.DICT)
    public List<SysLogPO> querySysLog(SysLogDTO dto) {
        if (dto != null && dto.getLogSys() == null ){
            throw BusinessException.build("日志系统不能为空...");
        }
        if (dto != null && dto.getStartTime() == null){
            //当前日期前30天
            dto.setStartTime(DateUtil.dateAddSubDay(new Date(),-30));
        }
        if (dto != null && dto.getEndTime() == null){
            //当前日期前30天
            dto.setEndTime(new Date());
        }

        return logMapper.selectLog(dto);

    }
}
